﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web.Security;

namespace Client
{
    public partial class LoginForm : Form
    {
        private int isalive = 0;
        public LoginForm()
        {
            InitializeComponent();
        }

        public LoginForm(UserSession session, UserData userdata, p2p.GlobalCmdBackground glCmdBg)
        {
            this.session = session;
            this.userdata = userdata;
            this.glCmdBg = glCmdBg;
            this.glCmdBg.CommandReceived += new p2p.CommandReceivedEventHandler(glCmdBg_CommandReceived);
            InitializeComponent();
            skinEngine1.SkinFile = "DeepCyan.ssk";
            isalive = 0;
        }

        private UserSession session;
        private UserData userdata;
        private p2p.GlobalCmdBackground glCmdBg;

        private void glCmdBg_CommandReceived(object sender, p2p.CommandEventArgs e)
        {
            p2p.Command cmd = e.Command;
            switch (cmd.CommandType)
            {
                case p2p.CommandType.UserHasAlreadyOnline:
                    if (cmd.SenderName == session.Name)
                    {
                        this.timer.Stop();
                        this.timer.Enabled = false;
                        this.labelWait.Visible = false;
                        this.Enabled = true;
                        MessageBox.Show("该用户已登录！");
                    }
                    break;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            isalive = 0;
            string UserName = this.textBoxName.Text.Trim();
            string UserPwd = this.textBoxPwd.Text;
            if (UserName == "" || UserPwd == "")
            {
                MessageBox.Show("请输入用户名和密码！");
                return;
            }
            //判断该用户是不是还没有设定密码
            userdata.ReadDb();
            if (userdata.Users.Count > 0)
            {
                foreach (KeyValuePair<string, UserData.User> user in this.userdata.Users)
                {
                    if (user.Value.UserName == UserName)
                    {
                        isalive = 1;
                        if (user.Value.UserPwd != "")
                        {
                            isalive = 2;
                            break;
                        }
                    }
                }
            }
            if (isalive==2)
            {
                //看看合适不合适，此处经过加密以后和DB文件进行对比，因为在服务端存储的Db中的密码是经过加密的
                string MD5;
                MD5 = FormsAuthentication.HashPasswordForStoringInConfigFile(UserPwd, "MD5");

                if (!userdata.Verify(UserName, MD5))
                {
                    MessageBox.Show("密码错误！");
                    return;
                }
            }
            else if(isalive==0)
            {
                MessageBox.Show("用户不存在！");
                return;
            }

            this.session.Name = UserName;
            this.session.Pwd = UserPwd;
            this.glCmdBg.SendCommand(new p2p.Command(p2p.CommandType.AskIfOnline,
                null,
                UserName,
                this.glCmdBg.MulticastEP,
                Encoding.Unicode.GetBytes(this.session.Name)));
            timer.Enabled = true;
            timer.Start();
            this.labelWait.Visible = true;
            this.Enabled = false;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Stop();
            this.timer.Enabled = false;
            this.DialogResult = DialogResult.OK;
            this.glCmdBg.CommandReceived -= new p2p.CommandReceivedEventHandler(glCmdBg_CommandReceived);
            this.Close();
        }
    }
}
